/**
 * 用户信息
 */
define(['$'],function(require, exports){
    var resp = {};
    var user = sessionStorage.login_date ? true : false;
    function check() {
        var login_page = /(login\.html|register\.html)/.test(location.pathname);

        if (user) {
            setInfo();
            if (login_page) {
                alert('您已登录成功');
                history.back(-1);
            }
        } else {
            $.ajax({
                url: '/Guest/Index/Index/',
                success: function (d) {

                    if (d == false || d=='false') {
                        //if (!login_page) location.href = '/page/types-hotelBook-1-login';
                    } else {
                        for(var i in d)
                            sessionStorage.setItem(i , d[i]);
                        setInfo();
                    }
                }
            })
        }
    }

    function logout(fn){
        $.ajax({
            url : '/Guest/Index/logout',
            success : function(d){
                sessionStorage.clear();
                fn && fn();
            }
        });
    }

    function info(){
        var d = sessionStorage;
        if(sessionStorage.length){
            return {
                'mobile': d.mobile,
                'email': d.email,
                'name': d.name
            }
        } else
            return null;
    }

    function setInfo(){
        var i = info();
        console.log(i);
        if(i == null) return;
        $('.j-user-name span').html(i['name']);
        $('.j-user-email span').html(i['email']);
        $('.j-user-mobile span').html(i['mobile']);
    }

    check();

    return {
        logout : logout,
        info : info
    };
})
